package com.rdgk.newtech.controller;

import com.alibaba.fastjson.JSONObject;
import com.rdgk.newtech.dao.ArticleDao;
import com.rdgk.newtech.service.ArticleService;
import com.rdgk.newtech.util.CommonUtil;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/excel")
public class ExcelController {

//    @Autowired
//    private TopicService topicService;
    @Autowired
    private ArticleService articleService;

    @Autowired
    private ArticleDao articleDao;

    @RequiresPermissions("article:list")
    @PostMapping("/importArticle")
    public JSONObject importArticle(MultipartFile file) throws IOException {
        System.out.println("ssssssssssssssssssssssssssssssssss");
//        return articleService.downExcel(httpServletResponse);

        // 创建HSSFWorkbook对象
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());

        // 获取一共有多少sheet,然后遍历
        int numberOfSheets = workbook.getNumberOfSheets();
        System.out.println("一共多少sheet:"+numberOfSheets);
        for (int i = 0; i < numberOfSheets; i++) {
            XSSFSheet sheet = workbook.getSheetAt(i);
            // 获取sheet中一共有多少行，遍历行（注意：第一行是标题）
            int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
            System.out.println("一共多少行："+ physicalNumberOfRows);
            JSONObject jsonObject = new JSONObject();
            for (int j = 0; j < physicalNumberOfRows; j++) {
                jsonObject.clear();
                if (j == 0) {
                    continue;//标题行
                }

                // 获取每一行有多少单元格，遍历单元格
                int physicalNumberOfCells = sheet.getRow(j).getPhysicalNumberOfCells();
                System.out.println("每一行有多少单元格:"+physicalNumberOfCells);
//                int physicalNumberOfCells = row.getPhysicalNumberOfCells();

                for (int k = 0; k < physicalNumberOfCells; k++) {
//                    XSSFCell cell = row.getCell(k);
                    XSSFCell cell = sheet.getRow(j).getCell(k);
//                    System.out.println("每一个单元格的内容："+cell);
//
//                    System.out.println("哈哈嘻啦啦："+cell.toString().equals("哈哈嘻啦啦"));
//                    System.out.println("1.0：" + cell.toString().equals("1.0"));

                    if (k == 0){
                        jsonObject.put("content",cell.toString());

                    }
                }
                System.out.println("jsonObject:"+jsonObject);
                articleDao.addArticle(jsonObject);
            }
        }
        return CommonUtil.successJson("上传成功");
    }

    @RequiresPermissions("article:list")
    @PostMapping("/importDingdan")
    public JSONObject importDingdan(MultipartFile file) throws IOException {
        System.out.println("ssssssssssssssssssssssssssssssssss");

        // 创建HSSFWorkbook对象
        XSSFWorkbook workbook = new XSSFWorkbook(file.getInputStream());

        // 获取一共有多少sheet,然后遍历
        int numberOfSheets = workbook.getNumberOfSheets();
        System.out.println("一共多少sheet:"+numberOfSheets);
        for (int i = 0; i < numberOfSheets; i++) {
            XSSFSheet sheet = workbook.getSheetAt(i);
            // 获取sheet中一共有多少行，遍历行（注意：第一行是标题）
            int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
            System.out.println("一共多少行："+ physicalNumberOfRows);
            JSONObject jsonObject = new JSONObject();
            for (int j = 0; j < physicalNumberOfRows; j++) {
                jsonObject.clear();
                if (j == 0) {
                    continue;//标题行
                }

                // 获取每一行有多少单元格，遍历单元格
                int physicalNumberOfCells = sheet.getRow(j).getPhysicalNumberOfCells();
                System.out.println("每一行有多少单元格:"+physicalNumberOfCells);
//                int physicalNumberOfCells = row.getPhysicalNumberOfCells();

                for (int k = 0; k < physicalNumberOfCells; k++) {
//                    XSSFCell cell = row.getCell(k);
                    XSSFCell cell = sheet.getRow(j).getCell(k);
//                    System.out.println("每一个单元格的内容："+cell);
//
//                    System.out.println("哈哈嘻啦啦："+cell.toString().equals("哈哈嘻啦啦"));
//                    System.out.println("1.0：" + cell.toString().equals("1.0"));
                }
                System.out.println("jsonObject:"+jsonObject);
//                articleDao.addArticle(jsonObject);
            }
        }

        return CommonUtil.successJson("上传成功");
    }

}
